package extractionStockageDonnees;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.jdom.DataConversionException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

/**
 * 
 * @author simon
 * 
 */
public class ParserCourse {
	// XML document to parse (GPX)
	private Document documentXML;
	// XML document's root
	private Element root;

	/**
	 * Constructor
	 * 
	 * @param fileName
	 */
	public ParserCourse(String pathFile) {
		// Make a new SAXBuilder object
		SAXBuilder sxb = new SAXBuilder();
		try {
			// Make a new JDOM document with the complete data's path
			this.documentXML = sxb.build(new File(pathFile));
		} catch (Exception e) {
			e.printStackTrace();
		}
		// Initialize the document root
		this.root = documentXML.getRootElement();
	}

	/**
	 * Method to convert a string to a float
	 * 
	 * @param s
	 * @return
	 */
	public static float strToFloat(String s) {
		float f = Float.valueOf(s.trim()).floatValue();
		return f;
	}

	/**
	 * Method to retrieve all points in the xml document
	 * 
	 * @return
	 * @throws DataConversionException
	 */
	public LinkedList<IStep> parseCourse() {
		// Initialize the xml document's root
		root = documentXML.getRootElement();
		// A Steps' List
		LinkedList<IStep> listSteps = new LinkedList<IStep>();

		// Make an steps' list
		List<?> listStepsElem = root.getChildren("rteType");
		// Make an iterator to scroll the steps' list
		Iterator<?> iterSteps = listStepsElem.iterator();
		// ***** We loop *****
		while (iterSteps.hasNext()) {
			// The current step
			Element currentStep = (Element) iterSteps.next();
			// Make an points' list
			List<?> listPointsElem = currentStep.getChildren("rtept");
			// Make an iterator to scroll the points' list
			Iterator<?> iterPoints = listPointsElem.iterator();
			// Make an LinkedList of points
			LinkedList<PointCourse> listPoints = new LinkedList<PointCourse>();
			// ***** We loop *****
			while (iterPoints.hasNext()) {
				// Current element (for each loop)
				Element currentElement = (Element) iterPoints.next();
				// Attributes of the current element
				float currentLatitude = strToFloat(currentElement.getAttribute(
						"lat").getValue());
				float currentLongitude = strToFloat(currentElement
						.getAttribute("lon").getValue());
				float currentElevation = strToFloat(currentElement.getChild(
						"ele").getValue());
				String currentName = currentElement.getChild("name").getValue();
				// Make a PointCourse
				PointCourse myCurrentPoint = new PointCourse(currentLatitude,
						currentLongitude, currentElevation, currentName);
				// We add the current PointCourse
				listPoints.add(myCurrentPoint);
			}
			// ***** End While Points *****
			// We find the type of the step
			String currentType = currentStep.getChild("extensions").getChild(
					"type").getValue();
			// We make the EType of the step
			// If it's a time step, we take the number of turns
			EType myType;
			int numberTurns = 0;
			if (currentType.equals("PLAIN")) {
				myType = EType.PLAIN;
			} else if (currentType.equals("MOUNTAIN")) {
				myType = EType.MOUNTAIN;
			} else {
				myType = EType.TIME;
				numberTurns = Integer.parseInt(currentStep.getChild(
						"extensions").getChild("turns").getValue());
			}
			// Make a Step
			Step myCurrentStep = new Step(listPoints, listPoints.getFirst()
					.getName(), listPoints.getLast().getName(), myType,
					numberTurns);
			// We add the current step to the list
			listSteps.add(myCurrentStep);
		}// ***** End While Step *****
		return listSteps;
	}
}
